package me.zhengjie.modules.transfer.factory.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.sequence.domain.Sequence;
import me.zhengjie.modules.sequence.service.SequenceService;
import me.zhengjie.modules.transfer.domain.TransferAc10;
import me.zhengjie.modules.transfer.domain.TransferAc11;
import me.zhengjie.modules.transfer.factory.service.TransferInformationService;
import me.zhengjie.modules.transfer.factory.service.TransferPersonService;
import me.zhengjie.modules.transfer.factory.service.TransferProcessService;
import me.zhengjie.modules.transfer.factory.vo.ContactInfo;
import me.zhengjie.modules.transfer.service.TransferAc10Service;
import me.zhengjie.modules.transfer.service.TransferAc11Service;
import me.zhengjie.tool.DateUtil;
import me.zhengjie.tool.MyMap;
import me.zhengjie.tool.NPETool;
import me.zhengjie.tool.ValidateUtil;
import me.zhengjie.vo.Constant;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class TransferProcessServiceImpl implements TransferProcessService {


    private final TransferAc10Service transferAc10Service;
    private final TransferAc11Service transferAc11Service;
    private final SequenceService sequenceService;

    protected TransferAc10 ac10;
    protected TransferAc11 ac11;

    @Override
    public Map createProcessTransfer(MyMap businessParam) {

        NPETool.isEmptyThrowException(businessParam.getAsString("aaz002"), "业务日志ID！");


        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("aaz002", businessParam.getAsString("aaz002"));
        List<Map> ac10 = transferAc10Service.listMaps(queryWrapper);
        if (ac10.size() > 0) {
            String aaz655 = ac10.get(0).get("aaz655").toString();
            businessParam.put("aaz655",aaz655);
            updateAc10(businessParam);
            createAc11(businessParam);



        } else {

            String aaz655 = createAc10(businessParam);
            businessParam.put("aaz655",aaz655);

            createAc11(businessParam);

        }


        return null;
    }

    @Override
    public Map deleteProcessTransfer(String aaz002) {

        //取消逻辑删除 方便记录日志
        NPETool.isEmptyThrowException(aaz002, "业务日志ID");
        UpdateWrapper<TransferAc10> update = new UpdateWrapper();
        update.set("aae802", Constant.AAE802_3_ZZ);
        update.eq("aaz002", aaz002);
        transferAc10Service.update(update);

      //  transferAc10Service.remove();
     //   transferAc11Service.removeByMap(query);
        return null;
    }


    @Override
    public List<Map<String, Object>> getProcessTransfer(String aaz002) {
        QueryWrapper<TransferAc10> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("aaz002",aaz002);
        return transferAc10Service.listMaps(queryWrapper);

    }

    /**
     * 组装ac10 add
     *
     * @param businessParam
     */
    public String createAc10(MyMap businessParam) {
        TransferAc10 transferAc10 = new TransferAc10();
        Sequence sequence_aaz655 = new Sequence();
        sequence_aaz655.setName("aaz655");
        String  aaz655=   sequenceService.nextSequence(sequence_aaz655);
        transferAc10.setAaz655(aaz655);
        transferAc10.setAae270(businessParam.getAsString("aae270"));
        transferAc10.setAac001(businessParam.getAsString("aac001"));
        transferAc10.setAaz159(businessParam.getAsString("aaz159"));
        transferAc10.setAac002(businessParam.getAsString("aac002"));
        transferAc10.setAac003(businessParam.getAsString("aac003"));
        transferAc10.setAae802(Constant.AAE802_1_CLZ);
        transferAc10.setAae013(businessParam.getAsString("aae013"));
        transferAc10.setAae011(businessParam.getAsString("aae011"));
        transferAc10.setAae036(new BigDecimal(DateUtil.getCurrentTime("yyyyMMddHHmmss")));
        transferAc10.setAab359(businessParam.getAsString("aab359"));
        transferAc10.setAaz002(businessParam.getAsString("aaz002"));
        transferAc10.setBac617(businessParam.getAsString("bac617"));
        transferAc10Service.save(transferAc10);
        return aaz655;


    }

    /**
     * 组装ac11
     *
     * @param businessParam
     */
    public void createAc11(MyMap businessParam){
        TransferAc11 transferAc11 = new TransferAc11();
        Sequence sequence_aaz656 = new Sequence();
        sequence_aaz656.setName("aaz656");
        String  aaz656=   sequenceService.nextSequence(sequence_aaz656);

        transferAc11.setAaz656(aaz656);
        transferAc11.setAaz655(businessParam.getAsString("aaz655"));
        transferAc11.setBae038(businessParam.getAsString("bae038"));
        transferAc11.setAae186(businessParam.getAsString("aae186"));
        transferAc11.setAae013(businessParam.getAsString("aae013"));
        transferAc11.setAae011(businessParam.getAsString("aae011"));
        transferAc11.setAae036(new BigDecimal(DateUtil.getCurrentTime("yyyyMMddHHmmss")));
        transferAc11.setAaz002(businessParam.getAsString("aaz002"));
        transferAc11Service.save(transferAc11);
    }

    /**
     * 组装ac10 update
     *
     * @param businessParam
     */
    public void updateAc10(MyMap businessParam) {
        UpdateWrapper<TransferAc10> updateWrapper = new UpdateWrapper();

        if (!ValidateUtil.isEmpty(businessParam.getAsBoolean("finish"))&& businessParam.getAsBoolean("finish") == true) {
            updateWrapper.set("aae802", Constant.AAE802_2_YBJ);
        } else {
            updateWrapper.set("aae802", Constant.AAE802_1_CLZ);
        }
         transferAc10Service.update(updateWrapper);

     }




    }
